package com.k.kc.util.date;

import com.k.kc.util.DateUtils;

import java.io.*;
import java.util.Calendar;

public class RiLiYearMonthDay {
    static final String[] WEEKS = new String[]{"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
    /**
     *
     * @param year year
     * @param month [1,12]
     * @return [0,6] 星期日，1，2，3，4，5，6
     */
    private static int getFirstWeekOfTheMonth(int year, int month) {
        if(month<1 || month>12){
            throw new IllegalArgumentException("month must be 1<= month <=12");
        }
        Calendar calender = Calendar.getInstance();
        calender.set(year, month-1, 1);
        return calender.get(Calendar.DAY_OF_WEEK)-1;
    }
    private static void printYearMonthDayWeek(int fromYear,int toYear,int fromMonth,int toMonth) throws IOException {
        File file = new File("E:/test/rili.csv");
        FileWriter fileWriter = new FileWriter(file);
        // csv need write bom in windows
        fileWriter.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));
        for(int year=fromYear;year<=toYear;year++){
            for(int month=fromMonth;month<=toMonth;month++){
                int monthDays = DateUtils.getDaysOfMonth(year, month);
                int firstDayWeek = getFirstWeekOfTheMonth(year,month);
                for(int day=1;day<=monthDays;day++){
                    String week = WEEKS[(day-1+firstDayWeek)%7];
                    String ymd = year+"-"+month+"-"+day;
                    String result = String.format("%s,%s,%s,%s,%s\r\n",ymd,year,month,day,week);
//                    System.out.print(result);
                    fileWriter.write(new String(result.getBytes("utf-8"),"utf-8"));
                }
            }
        }
        fileWriter.flush();
    }
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        printYearMonthDayWeek(2022,2050,1,12);
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}
